package Tree;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Tree
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-30  22:20
 * @Description: TODO
 * @Version: 1.0
 */
public class 找树左下角的值513 {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int maxDepth=Integer.MIN_VALUE;
    int result;
    public int findBottomLeftValue(TreeNode root) {
            if (root==null){
                return 0;
            }
            travel(root,1);
            return result;
    }
    public void travel(TreeNode root,int depth){
        if (root == null) {return;}
        // 遍历到叶子节就停止
        if (root.left==null&&root.right==null){
            if (depth>maxDepth){
                maxDepth=depth;
                result=root.val;
            }
        }
        if (root.left!=null){
            depth++;
            travel(root.left,depth);
            depth--;
        }
        if (root.right!=null){
            depth++;
            travel(root.right,depth);
            depth--;
        }
    }
}
